package com.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * @ClassName: Result
 * @Description: Swagger配置
 * @author: QT
 * @date: 2020年10月09日 上午9:26:37
 */
@Configuration
@EnableSwagger2 // 开启Swagger2的自动配置
public class SwaggerConfig {
    //Swagger-UI访问地址：http://localhost:8081/swagger-ui.html
    @Bean //配置docket的bean实例以配置Swagger具体参数
    public Docket docket(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true) //配置是否启用Swagger，如果是false，在浏览器将无法访问
                .select()
                //配置需要扫描的Package
                .apis(RequestHandlerSelectors.basePackage("com.api.controller"))
                // 配置如何通过path过滤
                //.paths(PathSelectors.ant("/api/**"))  //通过ant()控制，即这里只扫描请求以/api开头的接口
                //.paths(PathSelectors.any()) //任何请求都扫描
                //.paths(PathSelectors.none() //任何请求都不扫描
                //.paths(PathSelectors.regex("正则")) //通过正则表达式控制
                .build();
    }

    //配置文档信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("API-DEMO")
                .description("API-DEMO项目接口")
                .version("1.0")
                .build();
    }
}
